// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:wx.getStorageSync('banner'),
    https:wx.getStorageSync('https'),
    albums:'',    //预留获取相册信息
    WW:0,         //预留屏幕可用宽度
    arr_left:[],  //存放左右两列的数据
    keyword:'',   //搜索关键字
    arr_right:[],
    left_h:0,
    right_h:0,
    pnow:1,
    keyword_pnow:1,
    UserInfo:'',
    openid:'',
    Flower_Aids:'',    //记录当前用户点赞过的相册id
    Collect_Aids:''
  },


  

  //获取每个图片的宽高并保存到数据中
  LoadImage(e){
    var scale = (parseInt(e.detail.width) / parseInt(this.data.WW * 0.48)).toFixed(2);
    var arr = this.data.albums.list
    var arr_left = this.data.arr_left;   
    var arr_right = this.data.arr_right;
    for(var i=0;i<this.data.albums.list.length;i++){
      if(arr[i].album_id == e.target.dataset.id){
        var img_h = Math.round(parseInt(e.detail.height) / scale);
        var img_w = Math.round(parseInt(this.data.WW) * 0.48);
        arr[i].album_height= img_h;     //将图片宽高存入到图片信息中
        arr[i].album_width= img_w;
        if(this.data.left_h <= this.data.right_h){
          var left_h = this.data.left_h + img_h;
          var right_h = this.data.right_h;
          arr_left.push(arr[i])       //将数据分类并存入相就数组中
        }else{
          var left_h = this.data.left_h;
          var right_h = this.data.right_h + img_h;
          arr_right.push(arr[i])
        }
        this.setData({
          arr_left:arr_left,
          arr_right:arr_right,
          left_h:left_h,
          right_h:right_h
        });
      }
    }
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    wx.login({
      success:function(res){
        var code = res.code;
        wx.request({
          url:'https://photo.tobeyoung.net/NewAlbums/index.php/Home/Interface/Get_Openid',
          data:{
            code:code
          },
          success:function(res2){
              t.setData({
                openid:res2.data.openid,
              })
              t.Get_Window_Width(t);    //获取屏幕可用宽度
              t.Get_Album(t)    //获取图片
          }
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var t = this
      this.Get_Album(t)
  },



  //获取屏幕可用宽度
  Get_Window_Width(t){
    wx.getSystemInfo({
      success: function(res) {
        t.setData({
          WW:res.windowWidth
        })
      }
    })
  },




  //获取相册
  Get_Album(t){
    var t = this
    var pnow = this.data.pnow
    var keyword_pnow = this.data.keyword_pnow
    if(t.data.keyword){
      var data_obj={keyword:t.data.keyword,pnow:keyword_pnow}
    }else{
      var data_obj={pnow:pnow}
    }
    wx.request({
      url:t.data.https+'get_album_all',
      data:data_obj,
      success:function(res){
        if(res.data == 'err'){
          return
        }
        var c_stat;
        var f_stat;
        var data = res.data
        wx.request({
          url:t.data.https+'get_user_flower_collect',
          data:{
            openid:t.data.openid
          },
          success:function(res2){
            var Collects = res2.data.Collect_Aids
            var Flowers = res2.data.Flower_Aids
              for(var j=0;j<data.list.length;j++){
                if(Collects != null){
                  for(var i=0;i<Collects.length;i++){
                    if(data.list[j].album_id == Collects[i]){
                      c_stat = 1
                    }
                  }
                  if(c_stat == 1){
                      data.list[j].album_collect_stat=1
                  }else{
                      data.list[j].album_collect_stat=0
                  }
                  c_stat = 0
                }
                if(Flowers != null){
                  for(var i=0;i<Flowers.length;i++){
                    if(data.list[j].album_id == Flowers[i]){
                      f_stat = 1
                    }
                  }
                  if(f_stat == 1){
                      data.list[j].album_flower_stat=1
                  }else{
                      data.list[j].album_flower_stat=0
                  }
                  f_stat = 0
                }
              }
  

            if(t.data.keyword){
              t.setData({
                albums:data,
                keyword_pnow:t.data.keyword_pnow + 1,
              })   
            }else{
              t.setData({
                albums:data,
                pnow:t.data.pnow + 1,
              });
            }
          }
        })
      }
    });
  },



 



  //搜索表单
  formSubmit(e){
    var album_name = e.detail.value.album_name.trim()
    if(album_name == this.data.keyword){    //如果关键字没变则停止搜索
      return
    }
    this.setData({
      keyword:album_name,
      pnow:1,
      keyword_pnow:1,
      arr_left:[],
      arr_right:[],
      albums:''
    })
    var t = this
    this.Get_Album(t) 
  },


  //获取用户信息,并获取用户的收藏相册id
  getinfo(){
    var t = this
    wx.login({
      success:function(res1){
        var code = res1.code;
        wx.request({
          url:'https://photo.tobeyoung.net/NewAlbums/index.php/Home/Interface/Get_Openid',
          data:{
            code:code
          },
          success:function(res1){
                  wx.getUserInfo({
                      success: function(res) {
                          t.setData({
                            UserInfo:res.userInfo,
                            openid:res1.data.openid,
                          })
                          wx.setStorageSync('openid',res1.data.openid);
                          wx.setStorageSync('openid_exp',new Date().getTime());
                          wx.setStorageSync('UserInfo',res.userInfo);
                          wx.request({
                            url:t.data.https+'User_Login',
                            data:{
                              users_name:res.userInfo.nickName,
                              users_head:res.userInfo.avatarUrl,
                              openid:res1.data.openid
                            },
                            success:function(res3){
                            }
                          })
                  
                      }
                  })
          }
        })
        
      }
    })
  },

  getinfo2(){
    var t = this
    wx.getUserInfo({
      withCredentials:true,
      success: function(res) {
        t.setData({
          UserInfo:res.userInfo,
          openid:'abc',
        })
        wx.setStorageSync('openid',t.data.openid);
        wx.setStorageSync('UserInfo',res.userInfo);
        wx.request({
          url:t.data.https+'get_openid',
          data:{
            users_name:res.userInfo.nickName,
            users_head:res.userInfo.avatarUrl,
            code:'123'
          },
          success:function(res){
          }
        })
  
      }
    })
  },


  //点击 收藏 
  Collect(e){
    var t = this
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    wx.request({
      url:t.data.https+'set_collect',
      data:{
        aid:aid,
        openid:openid,
      },
      success:function(res){
        var left_stat;
        var right_stat;
        var arr_left = t.data.arr_left
        var arr_right = t.data.arr_right
        var collects = res.data;
        for(var i=0;i<arr_left.length;i++){
          for(var j=0;j<collects.length;j++){
            if(arr_left[i].album_id == collects[j]){
                left_stat = 1;
                continue;
            }
          }
          if(left_stat == 1){
            arr_left[i].album_collect_stat = 1
          }else{
            arr_left[i].album_collect_stat = 0 
          }
          left_stat = 0;
        }
        for(var i=0;i<arr_right.length;i++){
          for(var j=0;j<collects.length;j++){
            if(arr_right[i].album_id == collects[j]){
                right_stat = 1;
                continue;
            }
          }
          if(right_stat == 1){
            arr_right[i].album_collect_stat = 1
          }else{
            arr_right[i].album_collect_stat = 0 
          }
          right_stat = 0;
        }
        t.setData({
          arr_left:arr_left,
          arr_right:arr_right
        })
      }
    })
  },


  //点赞
  Flower(e){
    var t = this
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    wx.request({
      url:t.data.https+'set_flower',
      data:{
        aid:aid,
        openid:openid,
      },
      success:function(res){
        var left_stat;
        var right_stat;
        var arr_left = t.data.arr_left
        var arr_right = t.data.arr_right
        var flowers = res.data;
        for(var i=0;i<arr_left.length;i++){
          for(var j=0;j<flowers.length;j++){
            if(arr_left[i].album_id == flowers[j]){
                left_stat = 1;
                continue;
            }
          }
          if(left_stat == 1){
            arr_left[i].album_flower_stat = 1
          }else{
            arr_left[i].album_flower_stat = 0 
          }
          left_stat = 0;
        }
        for(var i=0;i<arr_right.length;i++){
          for(var j=0;j<flowers.length;j++){
            if(arr_right[i].album_id == flowers[j]){
                right_stat = 1;
                continue;
            }
          }
          if(right_stat == 1){
            arr_right[i].album_flower_stat = 1
          }else{
            arr_right[i].album_flower_stat = 0 
          }
          right_stat = 0;
        }
        wx.request({
          url:t.data.https+'Get_Album_Flowers',
          data:{
            aid:aid
          },
          success:function(res){
            var Flower_Count = res.data
            for(var i=0;i<arr_left.length;i++){
              if(aid == arr_left[i].album_id){
                arr_left[i].album_flower = Flower_Count
                t.setData({
                  arr_left:arr_left,
                  arr_right:arr_right
                })
                return
              }
            }
            for(var i=0;i<arr_right.length;i++){
              if(aid == arr_right[i].album_id){
                arr_right[i].album_flower = Flower_Count
                t.setData({
                  arr_left:arr_left,
                  arr_right:arr_right
                })
                return
              }
            }
          }
        });

        t.setData({
          arr_left:arr_left,
          arr_right:arr_right
        })
      }
    })
  },




})